local skel = fk.CreateSkill{
  name = "tea__yingshi",
  tags = { Skill.Compulsory }
}
Fk:loadTranslationTable{
  ["tea__yingshi"] = "应势",
  [":tea__yingshi"] = "锁定技，若你手牌中最近一次获得的牌仅有一张，你使用此牌效果结算两次；当你不因使用而失去此牌后，观看一名角色的手牌。",
  ["@@tea__yingshi-inhand"] = "应势",
  ["#tea__yingshi-choose"] = "应势：选择观看一名角色的手牌",
}
---@type TrigSkelSpec<UseCardFunc>
local yingshi = {
  can_trigger = function (self, event, target, player, data)
    if target == player and player:hasSkill(skel.name) and data.card then
      if data.card:isVirtual() then
        for _, id in ipairs(Card.static:getIdList(data.card)) do
          if Fk:getCardById(id):getMark("@@tea__yingshi-inhand") > 0 then
            return true
          end
        end
      else
        return data.card:getMark("@@tea__yingshi-inhand") > 0
      end
    end
  end,
  on_use = function (self, event, target, player, data)
    data.additionalEffect = (data.additionalEffect or 0) + 1
  end
}
---@type TrigSkelSpec<MoveCardsFunc>
local yingshi_adddata = {
  can_refresh = function(self, event, target, player, data)
    return not (data.extra_data or {}).teaYingshiIds and table.find(data, function(move)
      ---@diagnostic disable-next-line: return-type-mismatch
        return table.find(move.moveInfo, function(info)
          return Fk:getCardById(info.cardId):getMark("@@tea__yingshi-inhand") > 0
        end)
      end)
  end,
  on_refresh = function(self, event, target, player, data)
    local teaYingshiIds = {}
    for _, move in ipairs(data) do
      for _, info in ipairs(move.moveInfo) do
        if Fk:getCardById(info.cardId):getMark("@@tea__yingshi-inhand") > 0 then
          table.insert(teaYingshiIds, info.cardId)
        end
      end
    end

    data.extra_data = data.extra_data or {}
    data.extra_data.teaYingshiIds = teaYingshiIds
  end,
}
---@type TrigSkelSpec<MoveCardsFunc>
local yingshi_setmark = {
  can_refresh = function(self, event, target, player, data)
    if not player:hasSkill(skel.name) then return end
    for _, move in ipairs(data) do
      if move.to == player and move.toArea == Card.PlayerHand then
        if #move.moveInfo == 1 then
          event:setCostData(self, move.moveInfo[1].cardId)
          return true
        end
      end
    end
  end,
  on_refresh = function (self, event, target, player, data)
    local cid = event:getCostData(self)
    for _, id in ipairs(player:getCardIds("h")) do
      player.room:setCardMark(Fk:getCardById(id), "@@tea__yingshi-inhand", 0)
    end
    player.room:setCardMark(Fk:getCardById(cid), "@@tea__yingshi-inhand", 1)
  end
}
---@type TrigSkelSpec<MoveCardsFunc>
local yingshi_viewcards = {
  can_trigger = function(self, event, target, player, data)
    if not player:hasSkill(skel.name) or not (data.extra_data or {}).teaYingshiIds then return end
    for _, move in ipairs(data) do
      if move.moveReason ~= fk.ReasonUse and not (move.toArea == Card.PlayerHand and move.to == player) then
        return table.find(player.room.alive_players, function (p)
          return not p:isKongcheng()
        end)
      end
    end
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local to = room:askToChoosePlayers(player, {
      targets = table.filter(player.room.alive_players, function (p)
        return not p:isKongcheng()
      end),
      min_num = 1,
      max_num = 1,
      cancelable = false,
      prompt = "#tea__yingshi-choose",
      skill_name = skel.name
    })[1]
    room:doIndicate(player, { to })
    room:viewCards(player, {
      cards = to:getCardIds("h"),
      skill_name = skel.name,
    })
  end
}
skel:addEffect(fk.PreCardUse, yingshi)
skel:addEffect(fk.AfterCardsMove, yingshi_setmark)
skel:addEffect(fk.BeforeCardsMove, yingshi_adddata)
skel:addEffect(fk.AfterCardsMove, yingshi_viewcards)
return skel